package com.xiaoshangkou.smalltv2.ui.suggestion;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xiaoshangkou.smalltv2.databinding.FragmentHomeBinding;
import com.xiaoshangkou.smalltv2.databinding.FragmentSuggestionBinding;
import com.xiaoshangkou.smalltv2.thread.EmailThread;
import com.xiaoshangkou.smalltv2.ui.home.HomeViewModel;

public class SuggestionFragment extends Fragment {

    private SuggestionViewModel suggestionViewModel;
    private FragmentSuggestionBinding binding;
    private Thread thread;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        suggestionViewModel =
                new ViewModelProvider(this).get(SuggestionViewModel.class);

        binding = FragmentSuggestionBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        suggestionViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                Button suggestion = binding.suggestion;
                suggestion.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText edtOrderNoteText = binding.edtOrderNoteText;
                        String text=edtOrderNoteText.getText().toString();
                        thread = new Thread(new EmailThread(text));
                        thread.start();
                        Toast.makeText(getActivity(), "提交成功，感谢您的反馈与建议~", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}